from jsonrpc import dispatcher

from common import RpcResponse, RpcStatus
from common.exception import ServiceException, CheckedException
from service_rpc.api.basic_api import BasicMethod
from service_sms import DingtalkService, AlibabaSmsService


class RegisterMethod(BasicMethod):
    """
    注册rpc方法
    """

    @staticmethod
    @dispatcher.add_method
    def sent_custom_text_msg(msg, context=None):
        """
        发送自定义文本消息
        :param context: 上下文内容
        :param msg: 消息内容
        """
        try:
            context = BasicMethod.check_context(context)
        except CheckedException as ce:
            return RpcResponse.failure(ce.message)
        try:
            msg_api = DingtalkService.DingTalkMsgApi()
            msg_api.sent_custom_text_msg(context, msg)
        except ServiceException as es:
            return RpcResponse.failure(es.message)
        return RpcResponse.response_no_data(RpcStatus.SUCCESS_CODE, "文本消息已发送到钉钉")

    @staticmethod
    @dispatcher.add_method
    def sent_markdown_msg(title, msg, context=None):
        """
        发送Markdown消息
        :param context: 上下文内容
        :param title: 标题
        :param msg: 消息内容
        """
        try:
            context = BasicMethod.check_context(context)
        except CheckedException as ce:
            return RpcResponse.failure(ce.message)
        try:
            msg_api = DingtalkService.DingTalkMsgApi()
            msg_api.sent_markdown_msg(context, title, msg)
        except ServiceException as es:
            return RpcResponse.failure(es.message)
        return RpcResponse.response_no_data(RpcStatus.SUCCESS_CODE, "Markdown消息已发送到钉钉")

    @staticmethod
    @dispatcher.add_method
    def sent_image_link(image_url, context=None):
        """
        发送图像链接
        :param context: 上下文内容
        :param image_url: 图像的访问地址
        """
        try:
            context = BasicMethod.check_context(context)
        except CheckedException as ce:
            return RpcResponse.failure(ce.message)
        try:
            msg_api = DingtalkService.DingTalkMsgApi()
            msg_api.sent_image_link(context, image_url)
        except ServiceException as es:
            return RpcResponse.failure(es.message)
        return RpcResponse.response_no_data(RpcStatus.SUCCESS_CODE, "快照链接已发送到钉钉")

    @staticmethod
    @dispatcher.add_method
    def sent_image_msg(title, image_url, context=None):
        """
        发送图像预览图
        :param context: 上下文内容
        :param title: 标题
        :param image_url: 图片的访问地址
        """
        try:
            context = BasicMethod.check_context(context)
        except CheckedException as ce:
            return RpcResponse.failure(ce.message)
        try:
            msg_api = DingtalkService.DingTalkMsgApi()
            msg_api.sent_image_msg(context, title, image_url)
        except ServiceException as es:
            return RpcResponse.failure(es.message)
        return RpcResponse.response_no_data(RpcStatus.SUCCESS_CODE, "快照已发送到钉钉")

    @staticmethod
    @dispatcher.add_method
    def sent_file_msg(file_name, file_content, file_type="image", context=None):
        """
        发送文件
        :param context: 上下文内容
        :param file_name: 文件名称
        :param file_content: base64编码后的文件
        :param file_type: 文件类型 图像‘image’、音频‘voice’、视频‘video’、其他‘file’
        """
        try:
            context = BasicMethod.check_context(context)
        except CheckedException as ce:
            return RpcResponse.failure(ce.message)
        # 文件类型修正
        ft = BasicMethod.determine_file_type(file_name)
        if len(ft) > 0:
            file_type = ft
        try:
            msg_api = DingtalkService.DingTalkMsgApi()
            msg_api.sent_file_msg(context, file_name, file_content, file_type)
        except ServiceException as es:
            return RpcResponse.failure(es.message)
        return RpcResponse.response_no_data(RpcStatus.SUCCESS_CODE, "文件已发送到钉钉")

    @staticmethod
    @dispatcher.add_method
    def sent_error_msg(program_name, context=None):
        """
        发送程序异常消息
        """
        try:
            context = BasicMethod.check_context(context)
        except CheckedException as ce:
            return RpcResponse.failure(ce.message)
        try:
            msg_api = DingtalkService.DingTalkMsgApi()
            msg_api.sent_error_msg(context, program_name)
        except ServiceException as es:
            return RpcResponse.failure(es.message)
        return RpcResponse.response_no_data(RpcStatus.SUCCESS_CODE, "已发送钉钉通知")

    @staticmethod
    @dispatcher.add_method
    def sent_cease_msg(program_name, context=None):
        """
        发送程序停止消息
        """
        try:
            context = BasicMethod.check_context(context)
        except CheckedException as ce:
            return RpcResponse.failure(ce.message)
        try:
            msg_api = DingtalkService.DingTalkMsgApi()
            msg_api.sent_cease_msg(context, program_name)
        except ServiceException as es:
            return RpcResponse.failure(es.message)
        return RpcResponse.response_no_data(RpcStatus.SUCCESS_CODE, "已发送钉钉通知")

    @staticmethod
    @dispatcher.add_method
    def send_sms(template_code, params, context=None):
        """
        发送短息
        """
        try:
            context = BasicMethod.check_context(context)
        except CheckedException as ce:
            return RpcResponse.failure(ce.message)
        try:
            sms_api = AlibabaSmsService.AlibabaSmsApi()
            sms_api.send_sms(context, template_code, params)
        except ServiceException as es:
            return RpcResponse.failure(es.message)
        return RpcResponse.response_no_data(RpcStatus.SUCCESS_CODE, "已发送短信通知")

    @staticmethod
    @dispatcher.add_method
    def send_batch_sms(template_code, params, context=None):
        """
        批发短信
        """
        try:
            context = BasicMethod.check_context(context)
        except CheckedException as ce:
            return RpcResponse.failure(ce.message)
        try:
            sms_api = AlibabaSmsService.AlibabaSmsApi()
            sms_api.send_batch_sms(context, template_code, params)
        except ServiceException as es:
            return RpcResponse.failure(es.message)
        return RpcResponse.response_no_data(RpcStatus.SUCCESS_CODE, "短信已批量发送成功")
